package com.ruoyi.web.controller.core;

import java.util.List;

import com.ruoyi.core.domain.ExpressOrders;
import com.ruoyi.core.domain.Logistics;
import com.ruoyi.core.service.ILogisticsService;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.core.domain.Orders;
import com.ruoyi.core.service.IOrdersService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 快递发货Controller
 * 
 * @author lgc
 * @date 2020-08-10
 */
@Controller
@RequestMapping("/core/orders")
public class OrdersController extends BaseController
{
    private String prefix = "core/orders";

    @Autowired
    private IOrdersService ordersService;
    @Autowired
    private ISysUserService userService;
    @Autowired
    private ILogisticsService logisticsService;



    @RequiresPermissions("core:orders:view")
    @GetMapping()
    public String orders()
    {
        return prefix + "/orders";
    }

    /**
     * 查询快递发货列表
     */
    @RequiresPermissions("core:orders:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Orders orders)
    {
        orders.setCreatedBy(ShiroUtils.getUserId());
        startPage();
        List<ExpressOrders> list = ordersService.selectExpressOrdersList(orders);
        return getDataTable(list);
    }

    /**
     * 导出快递发货列表
     */
    @RequiresPermissions("core:orders:export")
    @Log(title = "快递发货", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Orders orders)
    {
        List<Orders> list = ordersService.selectOrdersList(orders);
        ExcelUtil<Orders> util = new ExcelUtil<Orders>(Orders.class);
        return util.exportExcel(list, "orders");
    }

    /**
     * 新增快递发货
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }


    /**
     * 修改快递发货
     */
    @GetMapping("/edit/{ordersId}")
    public String edit(@PathVariable("ordersId") String ordersId, ModelMap mmap)
    {
        Orders orders = ordersService.selectOrdersById(ordersId);
        orders.getProductId();
        mmap.put("orders", orders);
        return prefix + "/edit";
    }

    /**
     * 修改订单
     */
    @GetMapping("/update/{ordersId}")
    public AjaxResult update(@PathVariable("ordersId") Long ordersId)
    {
        return toAjax(ordersService.updateByOrdersId(ordersId,ShiroUtils.getUserId()));
    }



    /**
     * 修改保存快递发货
     */
    @RequiresPermissions("core:orders:edit")
    @Log(title = "订单修改", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    @Transactional(rollbackFor = {Exception.class})
    public AjaxResult editSave(Orders orders)
    {
        orders.setLastUpdatedBy(ShiroUtils.getUserId());
        AjaxResult ajaxResult = toAjax(ordersService.updateOrders(orders));
        return ajaxResult;
    }



    /**
     * 删除快递发货
     */
    @RequiresPermissions("core:orders:remove")
    @Log(title = "快递发货", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(ordersService.deleteOrdersByIds(ids));
    }


    /**
     * 跳转到发货页面
     * @param ordersId
     * @param mmap
     * @return
     */
    @RequiresPermissions("core:orders:edit")
    @GetMapping("/delivery/{ordersId}")
    public String delivery(@PathVariable("ordersId") String ordersId,ModelMap mmap){
        Orders orders = ordersService.selectOrdersById(ordersId);
        List<Logistics> logistics = logisticsService.selectLogisticsList(null);
        mmap.put("delivery", orders);
        mmap.put("logistics", logistics);
        return  prefix + "/delivery";
    }



    /**
     * 快递鸟 物流单号识别
     * @param expNo 物流单号
     * @return
     * @throws Exception
     */
    @ResponseBody
    @GetMapping("/KdApiOrderDistinguish/{expNo}")
    public AjaxResult KdApiOrderDistinguish(@PathVariable("expNo") String expNo) throws Exception {
        ordersService.KdApiOrderDistinguish(expNo);
        return ordersService.KdApiOrderDistinguish(expNo);
    }

    /**
     * 修改保存快递发货
     */
    @RequiresPermissions("core:orders:edit")
    @Log(title = "快递发货", businessType = BusinessType.UPDATE)
    @PostMapping("/delivery")
    @ResponseBody
    @Transactional(rollbackFor = {Exception.class})
    public AjaxResult delivery(Orders ordersDto)
    {
        SysUser sysUser = ShiroUtils.getSysUser();
        Long smsBalance = sysUser.getSmsBalance();
        Orders orders = ordersService.selectOrdersById(ordersDto.getOrdersId().toString());
        //订单修改
        orders.setExpressNumber(ordersDto.getExpressNumber());
        orders.setLogisticsName(ordersDto.getLogisticsName());
        orders.setLastUpdatedBy(ShiroUtils.getUserId());
        ordersService.updateByOrdersId(orders);
        //发货信息提醒
        ordersService.deliveryByIdAndNumber(orders, smsBalance);
        //用户使用SMS剩余减
        if (sysUser.getSmsBalance()>0){
            userService.updateUserBySmsBalance(sysUser);
            ShiroUtils.setSysUser(sysUser);
        }


        return AjaxResult.success();
    }


    @GetMapping("/info/{ordersId}")
    public String info(@PathVariable("ordersId") String ordersId, ModelMap mmap){
        Orders orders = ordersService.selectOrdersById(ordersId);
        mmap.put("delivery", orders);
        return  prefix + "/info";
    }

}
